<?php
interface ImporterClass {
    public function validate($params);
    public function getColumn($row, $columnName);
    public function step();
    public function setColumnList($columnList);
}

class Importer {
    private static $importerClasses = array();
    private static $importerObj;

    static function addImporter($extension, $className) {
        if (file_exists("_framework/importers/$className.php")) {
            Log::debug("Adding importer - $className");
            self::$importerClasses[$extension] = $className;
        }
    }

    static function init($fileName) {
        Log::debug("Initializing importer");
        self::$importerObj = getExposedObject("importer");
        if (self::$importerObj === false) {
            $result = serviceSuccessful(array());
            if (!file_exists($fileName)) { $result = serviceFailure(array(), "Error loading uploaded file"); }
            else {
                $fileparts = explode(".", $fileName);
                $ext = $fileparts[count($fileparts)-1];
                Log::debug("file extension is $ext");
                $importerClass = (self::$importerClasses[$ext] == "" ? "defaultimporter" : self::$importerClasses[$ext]);
                Log::debug("Importer class is $importerClass");
                include_once("importers/$importerClass.php");
                self::$importerObj = new $importerClass($fileName);
                exposeObject("importer", self::$importerObj);
                if (self::$importerObj instanceof ImporterClass) {
                    Log::debug("Using importer validation");
                } else {
                    throw new Exception("Invalid importer class type!");
                }
            }
        }
        Log::debug("Importer initialized");
    }

    static function setColumnList($columnList) {
        self::$importerObj->setColumnList($columnList);
        exposeObject("importer", self::$importerObj);
    }

    static function getNextRow() {
        $result = self::$importerObj->getNextRow();
        exposeObject("importer", self::$importerObj);
        return $result;
    }

    static function getcolumn($row, $columnName) {
        $result = self::$importerObj->getColumn($row, $columnName);
        exposeObject("importer", self::$importerObj);
        return $result;
    }

    static function validate($params) {
        Log::debug("starting validation");
        $result = self::$importerObj->validate($params);
        exposeObject("importer", self::$importerObj);
        Log::debug("Vallidation finished");
        return $result;
    }
}

class BaseImporter implements ImporterClass {
    protected $fileHandler;
    protected $currentRow = 0;
    protected $columnList = array();

    function __construct($fileName) {
        $this->fileHandler = fopen($fileName, "r");
    }
    
    function validate($params) {
        return serviceSuccessful(array());
    }

    function step() {
        $this->currentRow++;
    }

    function getNextRow() {
        if (feof($this->fileHandler)) { return false; }
        return fgets($this->fileHandler);
    }
    
    function getColumn($row, $columnName) {
        return "";
    }

    function setColumnList($columnList) {
        $this->columnList = $columnList;
    }
}

?>
